package com.cloudkinto.service.consumer.stock;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.cloudkinto.common.RabbitQueueConstant;
import com.cloudkinto.dto.order.outbound.SysStockDeductDto;
import com.cloudkinto.entity.OutboundPickingDo;
import com.cloudkinto.entity.ProductStockRackDo;
import com.cloudkinto.service.consumer.vo.StockToPlatformConsumerReq;
import com.cloudkinto.service.order.picking.OutboundPickingService;
import com.cloudkinto.service.platform.PlatformOrderAmazonSyncService;
import com.cloudkinto.service.platform.PlatformOrderRakutenSyncService;
import com.cloudkinto.service.platform.PlatformOrderWowmaSyncService;
import com.cloudkinto.service.platform.PlatformOrderYahooSyncService;
import com.cloudkinto.service.platform.qoo10.PlatformOrderQoo10Service;
import com.cloudkinto.service.platform.shopList.PlatformOrderSuperDeliverySyncService;
import com.cloudkinto.service.stock.ProductStockFlowingService;
import com.cloudkinto.service.stock.ProductStockRackService;
import com.cloudkinto.service.stock.ProductStockService;
import com.cloudkinto.vo.stock.ProductFlowingOutEt;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 库存消费者
 *
 * @author long
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class StockConsumer {

    private final ProductStockRackService productStockRackService;
    private final ProductStockFlowingService stockFlowingService;
    private final PlatformOrderRakutenSyncService platformOrderRakutenSyncService;
    private final PlatformOrderYahooSyncService platformOrderYahooSyncService;
    private final PlatformOrderAmazonSyncService platformOrderAmazonSyncService;
    private final PlatformOrderWowmaSyncService platformOrderWowmaSyncService;
    private final PlatformOrderQoo10Service platformOrderQoo10SyncService;
    private final PlatformOrderSuperDeliverySyncService platformOrderSuperDeliverySyncService;
    private final OutboundPickingService pickingService;
    private final ProductStockService productStockService;

    /**
     * 创建库存回传任务 分平台店铺
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.StockToPlatform_CreateStockToPlatformAfterSync))
    public void createStockToPlatformAfterSyncOrder(String msg) {
        try {
            productStockService.stockToPlatform(Long.parseLong(msg));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 库存扣减
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.Sys_Stock_Deduct))
    public void stockDeduct(String msg) {
        SysStockDeductDto sysStockDeductDto = JSONUtil.toBean(msg, SysStockDeductDto.class);
        OutboundPickingDo pickingDo = sysStockDeductDto.getPickingDo();
        ProductFlowingOutEt productFlowingOutEt = sysStockDeductDto.getProductFlowingOutEt();
        if (pickingDo == null || productFlowingOutEt == null) {
            return;
        }
        productStockRackService.lambdaUpdate()
                .eq(ProductStockRackDo::getId, pickingDo.getSubStockId())
                .setSql("lock_quantity = lock_quantity - " + pickingDo.getQty())
                .update();
        pickingService.removeById(pickingDo.getId());
        //库存流水
        stockFlowingService.insertProForOut(productFlowingOutEt);
    }


    /**
     * 库存回传至平台乐天
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.StockToPlatform_RakutenNew))
    public void stockToPlatformRakuten(String msg) {
        StockToPlatformConsumerReq req = JSON.parseObject(msg, StockToPlatformConsumerReq.class);
        try {
            platformOrderRakutenSyncService.setStockByStoreId(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 库存回传至平台雅虎
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.StockToPlatform_YahooNew))
    public void stockToPlatformYahoo(String msg) {
        StockToPlatformConsumerReq req = JSON.parseObject(msg, StockToPlatformConsumerReq.class);
        try {
            platformOrderYahooSyncService.setStockByStoreId(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 库存回传至平台亚马逊
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.StockToPlatform_AmazonNew))
    public void stockToPlatformAmazon(String msg) {
        StockToPlatformConsumerReq req = JSON.parseObject(msg, StockToPlatformConsumerReq.class);
        try {
            platformOrderAmazonSyncService.setStockByStoreId(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 库存回传至平台wowma
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.StockToPlatform_WowmaNew))
    public void stockToPlatformWowma(String msg) {
        StockToPlatformConsumerReq req = JSON.parseObject(msg, StockToPlatformConsumerReq.class);
        try {
            platformOrderWowmaSyncService.setStockByStoreId(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 库存回传至平台qoo10
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.StockToPlatform_Qoo10New))
    public void stockToPlatformQoo10(String msg) {
        StockToPlatformConsumerReq req = JSON.parseObject(msg, StockToPlatformConsumerReq.class);
        try {
            platformOrderQoo10SyncService.setStockByStoreId(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 库存回传至平台SuperDelivery
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.StockToPlatform_SuperDelivery))
    public void stockToPlatformSuperDelivery(String msg) {
        StockToPlatformConsumerReq req = JSON.parseObject(msg, StockToPlatformConsumerReq.class);
        try {
            platformOrderSuperDeliverySyncService.setStockByStoreId(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
